<?php

require SO_PATH . "code/Packet/Class/Init/so_config.class.php";
require SO_PATH . "code/Packet/Class/Init/g.class.php";
require SO_PATH . "code/Packet/Func/common.func.php";

class so_core {

    /**
     * 初始化
     */
    public function init() {
        $this->auto_define();
    }

    public function auto_define() {
        $this->_init_env();
        define("IS_SO", true);
        //网站Packet路径
        define('PACKET_PATH', SO_PATH . 'code/Packet/');
        //项目路径，不可更改
        define('APP_PATH', getcwd() . '/_' . APP_NAME . '/Code/');
        //定义缓存存放路径
        define("RUNTIME_PATH", getcwd() . '/_' . APP_NAME . '/Cache/');
    }

    private function _init_env() {
        $_G = array();
        $_G['PHP_SELF']     = dhtmlspecialchars($this->_get_script_url());
        $_G['basefilename'] = basename($_G['PHP_SELF']);

        //网站目录
        $sitepath = substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], '/'));

        $_G['siteurl'] = dhtmlspecialchars('http://' . $_SERVER['HTTP_HOST'] . $sitepath . '/');

        //解析网站目录
        $url = parse_url($_G['siteurl']);

        $_G['siteroot'] = isset($url['path']) ? $url['path'] : '';
        $_G['siteport'] = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT'];
        G::$Cache = $_G;
    }

    function build_app_dir() {
        // 没有创建项目目录的话自动创建
        if (!is_dir(APP_PATH))
            mkdir(APP_PATH, 0755, true);
        if (is_writeable(APP_PATH)) {
            $dirs = array(
                LIB_PATH,
                RUNTIME_PATH,
                CONF_PATH,
                COMMON_PATH,
                LANG_PATH,
                CACHE_PATH,
                LOG_PATH,
                TEMP_PATH,
                DATA_PATH,
                LIB_PATH . 'Model/',
                LIB_PATH . 'Action/',
                LIB_PATH . 'Behavior/',
                LIB_PATH . 'Widget/',
                APP_PATH . 'Modules/',
                APP_PATH . 'Modules/Home/',
                APP_PATH . 'Modules/Home/Action/',
                APP_PATH . 'Modules/Admin/',
                APP_PATH . 'Modules/Admin/Action/',
            );
            foreach ($dirs as $dir) {
                if (!is_dir($dir))
                    mkdir($dir, 0755, true);
            }
            // 写入目录安全文件
            build_dir_secure($dirs);
            so_core::build_auto_file();
        }else {
            header('Content-Type:text/html; charset=utf-8');
            exit('项目目录不可写，目录无法自动生成！<BR>请使用项目生成器或者手动生成项目目录~');
        }
    }

    public function build_auto_file() {
        $file = array(
            APP_PATH . 'Modules/Home/Action/IndexAction.class.php' => PACKET_PATH . 'Extend/BuildTpl/default_index.tpl',
            CONF_PATH . 'config.php'                               => PACKET_PATH . 'Extend/BuildTpl/default_config.tpl',
            CONF_PATH . 'alias.php'                                => PACKET_PATH . 'Extend/BuildTpl/default_alias.tpl',
            CONF_PATH . 'tags.php'                                 => PACKET_PATH . 'Extend/BuildTpl/default_tags.tpl',
        );
        foreach ($file as $key => $value) {
            if (!is_file($key)) {
                $content = file_get_contents($value);
                file_put_contents($key, $content);
            }
        }
    }

    private function _get_script_url() {
        if (!isset($this->var['PHP_SELF'])) {
            $scriptName = basename($_SERVER['SCRIPT_FILENAME']);
            if (basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
                $this->var['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
            } else if (basename($_SERVER['PHP_SELF']) === $scriptName) {
                $this->var['PHP_SELF'] = $_SERVER['PHP_SELF'];
            } else if (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
                $this->var['PHP_SELF'] = $_SERVER['ORIG_SCRIPT_NAME'];
            } else if (($pos                   = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) {
                $this->var['PHP_SELF'] = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName;
            } else if (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) {
                $this->var['PHP_SELF'] = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']));
                $this->var['PHP_SELF'][0] != '/' && $this->var['PHP_SELF'] = '/' . $this->var['PHP_SELF'];
            } else {
                system_error('request_tainting');
            }
        }
        return $this->var['PHP_SELF'];
    }

}
